/*
 * createAccountWindow.java
 *
 * Created on 11. Dezember 2003, 19:10
 */

/**
 *
 * @author  christoph
 */

package info.chrm.jtalker;

import info.chrm.helper.*;

public class CreateAccountWindow extends javax.swing.JDialog {
    JTalkerClientFront parent = null;
    
    /** Creates new form createAccountWindow */
    public CreateAccountWindow(JTalkerClientFront _parent, boolean _modal) {
        super(_parent, _modal);
	this.parent = _parent;
        this.initComponents();
        this.getRootPane().setDefaultButton(this.jButton1); 
	this.setLocationRelativeTo(_parent);
	this.setResizable(false);
	this.setTitle("Create JTalker Account");
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        ClassLoader cl = ResourceAnchor.class.getClassLoader();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

	jLabel1.setIcon(new javax.swing.ImageIcon(cl.getResource("icons/new_account.jpg")));
        jLabel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel1.add(jLabel1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);

        jPanel2.setLayout(null);

        jPanel2.setPreferredSize(new java.awt.Dimension(300, 195));
        jLabel2.setFont(new java.awt.Font("Dialog", 1, 18));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Create Account");
        jPanel2.add(jLabel2);
        jLabel2.setBounds(40, 10, 220, 25);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("User Name:");
        jPanel2.add(jLabel3);
        jLabel3.setBounds(10, 60, 110, 18);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Password:");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(10, 90, 110, 18);

        jTextField1.setToolTipText("A new user name");
	jTextField1.setText(System.getProperty("user.name"));
        jPanel2.add(jTextField1);
        jTextField1.setBounds(130, 60, 150, 22);

        jPasswordField1.setToolTipText("Password");
        jPanel2.add(jPasswordField1);
        jPasswordField1.setBounds(130, 90, 150, 22);

        jButton1.setText("Create");
        jButton1.setToolTipText("Connects to server and trys to create an account");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel2.add(jButton1);
        jButton1.setBounds(190, 130, 88, 28);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel2.add(jLabel5);
        jLabel5.setBounds(10, 170, 270, 20);

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Default Account");
        jCheckBox1.setToolTipText("Writes the name and passwd to your JTalker config file");
        jPanel2.add(jCheckBox1);
        jCheckBox1.setBounds(10, 130, 170, 26);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Create Account
        this.jLabel5.setText("Checking inputs...");
        String new_name = this.jTextField1.getText();
        String new_pass = new String(this.jPasswordField1.getPassword());
        
        if(new_name.length() < 1 || new_pass.length() < 1) {
            this.jLabel5.setText("Name or password to short!");
            return;
        }

	if(new_name.indexOf(";;") != -1 || 
	      new_name.indexOf(" AT ") != -1 || 
	      new_name.indexOf("<") != -1 ||
	      new_name.indexOf(">") != -1 || 
	      new_name.indexOf("\"") != -1) {
            this.jLabel5.setText("Forbidden characters in name!");
            return;
	}
                
        // Call Account creation procedure
	if(this.parent.isConnected)
	   this.parent.connectActionPerformed(null);
	
	if(!this.parent.theClient.createNewAccount(new_name, new_pass)) {
            this.jLabel5.setText("Can't create Account on " + this.parent.theClient.serverName);  
	    return;
	} else {
	    if(this.jCheckBox1.isSelected()) {
	       this.parent.theClient.userName = new_name;
	       this.parent.theClient.userPasswd = new_pass;
               this.parent.theClient.savePassword = true;
	       this.parent.theClient.saveSettings();
	       this.parent.isConnected = this.parent.theClient.isConnected;
	    }
	    this.closeDialog(null);
	}
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        new CreateAccountWindow(new javax.swing.JFrame(), true).show();
    }*/
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    
}
